//----------------------------------------------------------------------------------------------------------------------
// Copyright (c) 2012 James Whitworth
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the \"Software\"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//----------------------------------------------------------------------------------------------------------------------

local file = vargv[0]
if (type(file) != "instance" || file.getclass() != File)
{
  throw "expected instance of class File"
}

file.header_includes.append("gmock/gmock.h")
file.header_includes.append("sqbind/sqbTypeTraits.h")
file.header_includes.append("../../TypeHelpers.h")
file.include_path = ""

enum FunctionTypes
{
  kMockFunction,
  kMockConstFunction,
  kFunction,
}

local kVoidReturnType = Parameter()
local kReturnType = Parameter("BoundClass")
local kInvalidReturnType = Parameter("InvalidStackUtilsClass")

local add_function = function(
  mock_class,
  function_name,
  function_type,
  return_type,
  parameter_count)
{
  local base_function_name = format("%s%d", function_name, parameter_count)

  local member_function = null
  switch (function_type)
  {
  case FunctionTypes.kMockFunction:
    member_function = MockFunction(format("Class%s", base_function_name))
    break;
  case FunctionTypes.kMockConstFunction:
    member_function = MockFunction(format("ConstClass%s", base_function_name), CodeGenerator.kConst)
    break;
  case FunctionTypes.kFunction:
    member_function = Function(base_function_name, CodeGenerator.kStatic)
    break;
  default:
    assert(false)
  }

  member_function.return_type = return_type

  local parameters = ""
  if (parameter_count > 0)
  {
    member_function.parameters.append(Parameter("BoundClass", "parameter0"))
    parameters = "parameter0"

    for (local i = 1; i < parameter_count; ++i)
    {
      local parameter_name = format("parameter%d", i)
      member_function.parameters.append(Parameter("BoundClass", parameter_name))
      parameters = format("%s, %s", parameters, parameter_name)
    }
  }
  
  if (function_type == FunctionTypes.kFunction)
  {
    member_function.body.add_line("return m_instance->Class%s(%s);", base_function_name, parameters)
  }

  mock_class.public.functions.append(member_function)
}

//----------------------------------------------------------------------------------------------------------------------
local arguments = _OPTIONS["arguments"]

local mock_class_name = format("MockFunction%d", arguments)
local mock_class = Class(mock_class_name)

local static_instance = Parameter(mock_class_name, "m_instance", CodeGenerator.kStatic | CodeGenerator.kPointer)
static_instance.value = "nullptr"
mock_class.public.parameters.append(static_instance)

local constructor = Function(mock_class_name)
constructor.return_type = NullParameter()
mock_class.public.functions.append(constructor)

local destructor = Function(format("~%s", mock_class_name))
destructor.return_type = NullParameter()
mock_class.public.functions.append(destructor)

add_function(mock_class, "VoidFunction", FunctionTypes.kMockFunction, kVoidReturnType, arguments)
add_function(mock_class, "Function", FunctionTypes.kMockFunction, kReturnType, arguments)
add_function(mock_class, "InvalidFunction", FunctionTypes.kMockFunction, kInvalidReturnType, arguments)

add_function(mock_class, "VoidFunction", FunctionTypes.kMockConstFunction, kVoidReturnType, arguments)
add_function(mock_class, "Function", FunctionTypes.kMockConstFunction, kReturnType, arguments)
add_function(mock_class, "InvalidFunction", FunctionTypes.kMockConstFunction, kInvalidReturnType, arguments)

add_function(mock_class, "VoidFunction", FunctionTypes.kFunction, kVoidReturnType, arguments)
add_function(mock_class, "Function", FunctionTypes.kFunction, kReturnType, arguments)
add_function(mock_class, "InvalidFunction", FunctionTypes.kFunction, kInvalidReturnType, arguments)

file.contents.append(mock_class)

// add the typedef for the fixture name
//
local non_copyable_declaration = CustomFileContent()
non_copyable_declaration.header_data = format("SQBIND_TYPE_NON_COPYABLE(::autogen::%s);", mock_class_name)
file.external_contents.append(non_copyable_declaration)
local typeinfo_declaration = CustomFileContent()
typeinfo_declaration.header_data = format("SQBIND_DECLARE_TYPEINFO(::autogen::%s, %s);", mock_class_name, mock_class_name)
file.external_contents.append(typeinfo_declaration)